package com.bw.park2301.notice

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.annotation.IdRes
import androidx.annotation.IntRange
import androidx.databinding.DataBindingUtil
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.DiffUtil.ItemCallback
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.bw.lib_base.entity.NoticeBeanItem
import com.bw.park2301.R
import com.bw.park2301.databinding.ItemNoticeBinding
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemLongClickListener
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import java.lang.ref.WeakReference

//diffCallback  更局部的刷新
/**
 * NoticeAdapter 是一个用于展示通知列表的适配器，继承自 PagingDataAdapter
 * 它负责管理通知数据项的展示和更新
 */
class NoticeAdapter(private val recyclerView: RecyclerView): PagingDataAdapter<NoticeBeanItem, NoticeAdapter.NoticeViewHolder>(diff) {
    private var mOnItemClickListener: OnItemClickListener? =null
    private var mOnItemLongClickListener: OnItemLongClickListener? =null
    lateinit var weakRecyclerView: WeakReference<RecyclerView>
    init {
        weakRecyclerView = WeakReference(recyclerView)
    }
    companion object {
        // diff 对象用于比较通知数据项的差异，以优化适配器的更新性能
        val diff = object : ItemCallback<NoticeBeanItem>() {
            /**
             * 判断两个通知数据项是否为同一个
             * @param oldItem 旧的通知数据项
             * @param newItem 新的通知数据项
             * @return 如果两个数据项相同返回 true，否则返回 false
             */
            override fun areItemsTheSame(
                oldItem: NoticeBeanItem,
                newItem: NoticeBeanItem
            ): Boolean {
                return oldItem == newItem
            }

            /**
             * 判断两个通知数据项的内容是否相同
             * @param oldItem 旧的通知数据项
             * @param newItem 新的通知数据项
             * @return 如果两个数据项的 id 相同返回 true，否则返回 false
             */
            override fun areContentsTheSame(
                oldItem: NoticeBeanItem,
                newItem: NoticeBeanItem
            ): Boolean {
                return oldItem.id == newItem.id
            }
        }
    }

    /**
     * NoticeViewHolder 是通知列表项的视图持有器
     * 它负责将数据绑定到列表项的视图上
     */
    class NoticeViewHolder(view: View): ViewHolder(view) {
        val binding = DataBindingUtil.bind<ItemNoticeBinding>(view)
    }

    /**
     * 将通知数据项绑定到视图持有器上
     * @param holder 视图持有器
     * @param position 数据项的位置
     */
    override fun onBindViewHolder(holder: NoticeViewHolder, position: Int) {
        val item = getItem(position)
        holder.binding?.bean = item
        holder.itemView.setOnClickListener {
            this.mOnItemClickListener?.onItemClick(position, item!!)
        }
        holder.itemView.setOnLongClickListener {
            this.mOnItemLongClickListener?.onItemLongClick(item!!)
            return@setOnLongClickListener true
        }
    }

    /**
     * 创建新的视图持有器
     * @param parent 视图持有器的父视图
     * @param viewType 视图类型
     * @return 返回新的 NoticeViewHolder 实例
     */
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): NoticeViewHolder {
        val view =
            LayoutInflater.from(parent.context).inflate(R.layout.item_notice, parent, false)
        return NoticeViewHolder(view)
    }
    fun setOnItemClickListener(listener: OnItemClickListener){
        this.mOnItemClickListener = listener
    }
    fun setOnItemLongClickListener(listener: OnItemLongClickListener?) {
        this.mOnItemLongClickListener = listener
    }
    interface OnItemClickListener {
        fun onItemClick(position: Int, item: NoticeBeanItem)
    }
    interface OnItemLongClickListener {
        fun onItemLongClick(item: NoticeBeanItem): Boolean
    }

    fun getViewByPosition(position: Int, @IdRes viewId: Int): View? {
        val recyclerView = weakRecyclerView.get() ?: return null
        val viewHolder = recyclerView.findViewHolderForLayoutPosition(position) as NoticeViewHolder?
            ?: return null
        return if (viewId == null){
            viewHolder.itemView
        }else{
            viewHolder.itemView.findViewById(viewId)
        }
    }
}
